/*************************************************************************************************\
*																																																	*
* PROGRAM: Sensors    	      																																		*
* VERSION: 0																																											*
* PURPOSE: This program provides basic functuionality for working with sensors.                   *
* AUTHOR:	 Aswin Bouwmeester (aswin.bouwmeester@gmail.com)																				*
* DATE:		 okt 10, 2010	  																																				*
*																																																	*
\*************************************************************************************************/

#define _SENSORS_

#pragma systemFile

#ifndef _TLIST_
  #include "tList.c";
#endif

float filteredSensorValue[4];

tSensors findSensor(TSensorTypes type);
float SensorValueStat(tSensors s, int interval, int N, Tstatistics stat);
void getSensorValues(tList &list,tSensors s, int interval, int N);


// automaticly find to what port a sensor is attached (returns S5 when sensor is not found)
tSensors findSensor(TSensorTypes type)
{
  tSensors port;
  for (port=S1;port<=S4;port++)
    if (SensorType[port]==type) return port;
  return S5;
}

// get a statistic of a number of sensor readings;
float SensorValueStat(tSensors s, int interval, int N, Tstatistics stat)
{
  tList raw;
  raw.maxlength=N;
  raw.length=0;
  getSensorValues(&raw,s,interval,N);
  return getListStat(raw,stat);
}

// fill a list with sensor readings;
void getSensorValues(tList &list,tSensors s, int interval, int N)
{
    // function is limited to max 20 sensor readings;
  if (N>TLISTLENGTH)
  {
    PlaySound(soundException);
    nxtDisplayTextLine(1,"E: getSensorValues");
    while(nNxtButtonPressed==-1);
  }
  else
  {
    for (int i=0;i<N;i++)
    {
      addItem(list,SensorValue[s]);
      wait1Msec(interval);
    }
  }
}
